<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Model;

class IndexController extends BaseController
{

    public function __construct()
    {
        parent::__construct();
    }

    //用户管理
    public function index()
    {
        $statu = I('get.statu');
        if(!empty($statu))
        {
            $_SESSION['statu'] = $statu;
        }
        $type = I('get.type');
        $nickname = I('post.nickname');
        $sex = I('post.sex');
        if(!empty($sex))
        {
            $_SESSION['sex'] = $sex;
        }
        $model = new Model();
        $sql = "select a.*,b.nickname,b.sex,b.header_pic,d.region_name from wl_user a
       LEFT JOIN wl_userinfo b on a.id=b.user_id LEFT JOIN wl_region c on b.city=c.id LEFT JOIN wl_region d on c.parent_id=d.id where 1=1";
        if (!empty($nickname)) {
            $sql .= " and b.nickname like '%$nickname%'";
        }
        if (!empty($type)) {
            $this->assign('type', $type);
            $sql .= " and DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(a.register_time)";
        }
        if (!empty($statu)||!empty($_SESSION['statu'])) {
            $status = !empty($statu)?$statu:$_SESSION['statu'];
            $sql .= " and a.statu=$status";
        }
        if(!empty($sex)||!empty($_SESSION['sex']))
        {
            $sexs = !empty($sex)?$sex:$_SESSION['sex'];
            $sql.= " and a.type!=5 and b.sex=$sexs";
        }
        $sql.=" order by a.id desc";
        $data = $model->query($sql);
        $count = count($data);
        $Page = new \Think\Page($count, 10);// 实例化分页类
        $show = $Page->show();// 分页显示输出
        $sql .= " limit $Page->firstRow,$Page->listRows";
        $list = $model->query($sql);
        $this->assign('data', $list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
        $this->assign('page', $show);
        $this->display();
    }

    //屏蔽用户
    public function pingbi()
    {
        $id = I('post.id');
        $model = M('user');
        $condition['id'] = $id;
        //$data['type'] = 10;
        $data['ifpingbi']=1;
        $res = $model->where($condition)->save($data);
        if(!empty($res))
        {
            $result = array(
                'statu'=>1,
                'msg'=>'update is ok!',
            );
        }else{
            $result = array(
                'statu'=>2,
                'msg'=>'update is failed!',
            );
        }
        $this->ajaxReturn($result);
    }
    //取消屏蔽 --碎月新增
    public function chalepingbi(){
    	$id = I('post.id');
    	$model = M('user');
    	$condition['id'] = $id;
    	  $data['ifpingbi']=2;
    	$res = $model->where($condition)->save($data);
    	if(!empty($res))
    	{
    		$result = array(
    				'statu'=>1,
    				'msg'=>'update is ok!',
    		);
    	}else{
    		$result = array(
    				'statu'=>2,
    				'msg'=>'update is failed!',
    		);
    	}
    	$this->ajaxReturn($result);
    }
    //设置优质用户
    public function youzhi(){
    	$id = I('post.id');
    	$model = M('user');
    	$condition['id'] = $id;
    	$data['ifhighgrade']=1;
    	$res = $model->where($condition)->save($data);
    	if(!empty($res))
    	{
    		$result = array(
    				'statu'=>1,
    				'msg'=>'update is ok!',
    		);
    	}else{
    		$result = array(
    				'statu'=>2,
    				'msg'=>'update is failed!',
    		);
    	}
    	$this->ajaxReturn($result);
    }
    //取消优质用户
    public function chaleyouzhi(){
    	$id = I('post.id');
    	$model = M('user');
    	$condition['id'] = $id;
    	$data['ifhighgrade']=2;
    	$res = $model->where($condition)->save($data);
    	if(!empty($res))
    	{
    		$result = array(
    				'statu'=>1,
    				'msg'=>'update is ok!',
    		);
    	}else{
    		$result = array(
    				'statu'=>2,
    				'msg'=>'update is failed!',
    		);
    	}
    	$this->ajaxReturn($result);
    }
    
    //批量删除用户
    public function delete_all()
    {
        $id = I('post.id');
        $model = D('user');
        $where['id'] = array('in', $id);
        $res = $model->where($where)->delete();
        $umodel = D('userinfo');
        $uwhere['user_id'] = array('in', $id);
        $result = $umodel->where($uwhere)->delete();
        if (!empty($res) && !empty($result)) {
            $result = array(
                'statu' => 1
            );
        } else {
            $result = array(
                'statu' => 2
            );
        }
        $this->ajaxReturn($result);
    }


    /*
     * 认证管理
     * @type string
     * type不穿为空显示全部用户，type=1则是未认证用户,type=2显示认证过的用户
     */
    public function auth_manage()
    {
        $nickname = I('post.nickname');
        $type = I('get.type');
        $statu = I('get.statu');
        $model = new Model();
        $sql = "select a.*,b.nickname,b.sex,b.header_pic,c.region_name,e.pic_url as shouchi,f.pic_url as zhengmian
       from wl_user a LEFT JOIN wl_userinfo b on a.id=b.user_id LEFT JOIN wl_region c on b.city=c.id
       LEFT JOIN wl_images e on a.id=e.user_id and e.type=1 LEFT JOIN wl_images f on a.id=f.user_id and f.type=11
       where b.header_pic is not null ";
        if (!empty($type)) {
            $sql .= " and a.type=$type";
        }
        if (!empty($nickname)) {
            $sql .= " and b.nickname like '%$nickname%'";
        }
        if (!empty($statu)) {
            $sql .= " and a.statu=$statu";
        }
        
        $sql.=" GROUP BY a.id";
        $sql.=" order by a.register_time desc";
        $data = $model->query($sql);
        $count = count($data);
        $Page = new \Think\Page($count, 10);// 实例化分页类
        $show = $Page->show();// 分页显示输出
        $sql .= " limit $Page->firstRow,$Page->listRows";
        $list = $model->query($sql);
        $this->assign('data', $list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
        $this->assign('page', $show);
        $this->display();
    }

    //房屋认证
    public function house_auth_manage()
    {
        $nickname = I('post.nickname');
        $type = I('get.type');
        $statu = I('get.statu');
        $model = new Model();
        $sql = "select a.*,b.nickname,b.sex,b.header_pic,b.house_auth,d.region_name,e.pic_url as fangchan
       from wl_user a LEFT JOIN wl_userinfo b on a.id=b.user_id LEFT JOIN wl_region c on b.city=c.id
       LEFT JOIN wl_region d on c.parent_id=d.id LEFT JOIN wl_images e on a.id=e.user_id and e.type=3 where e.pic_url!=''
        		GROUP BY a.id
        		";
        //增加搜索条件
        if (!empty($type)) {
            $sql .= " and b.house_auth=$type";
        }
        if (!empty($nickname)) {
            $sql .= " and b.nickname like '%$nickname%'";
        }
        if (!empty($statu)) {
            $sql .= " and a.statu=$statu";
        }
        $sql.=" order by a.register_time desc";
        $data = $model->query($sql);
        $count = count($data);
        $Page = new \Think\Page($count, 10);// 实例化分页类
        $show = $Page->show();// 分页显示输出
        $sql .= " limit $Page->firstRow,$Page->listRows";
        $list = $model->query($sql);
        $this->assign('data', $list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
//        var_dump($list);
        $this->assign('page', $show);
        $this->display();
    }


    //车辆认证
    public function car_auth_manage()
    {
        $nickname = I('post.nickname');
        $type = I('get.type');
        $statu = I('get.statu');
        $model = new Model();
        $sql = "select a.*,b.nickname,b.sex,b.header_pic,b.car_auth,d.region_name,e.pic_url as car
       from wl_user a LEFT JOIN wl_userinfo b on a.id=b.user_id LEFT JOIN wl_region c on b.city=c.id
       LEFT JOIN wl_region d on c.parent_id=d.id LEFT JOIN wl_images e on a.id=e.user_id and e.type=4
        		where e.pic_url!='' group by a.id";
        //增加搜索条件
        if (!empty($type)) {
            $sql .= " and b.car_auth=$type";
        }
        if (!empty($nickname)) {
            $sql .= " and b.nickname like '%$nickname%'";
        }
        if (!empty($statu)) {
            $sql .= " and a.statu=$statu";
        }
        $sql.= " order by a.register_time desc";
        $data = $model->query($sql);
        $count = count($data);
        $Page = new \Think\Page($count, 10);// 实例化分页类
        $show = $Page->show();// 分页显示输出
        $sql .= " limit $Page->firstRow,$Page->listRows";
        $list = $model->query($sql);
        $this->assign('data', $list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
//        var_dump($list);
        $this->assign('page', $show);
        $this->display();
    }

    //学历认证
    public function education_auth_manage()
    {
        $nickname = I('post.nickname');
        $type = I('get.type');
        $statu = I('get.statu');
        $model = new Model();
        $sql = "select a.*,b.nickname,b.sex,b.header_pic,b.education_auth,d.region_name,e.pic_url as education
       from wl_user a LEFT JOIN wl_userinfo b on a.id=b.user_id LEFT JOIN wl_region c on b.city=c.id
       LEFT JOIN wl_region d on c.parent_id=d.id LEFT JOIN wl_images e on a.id=e.user_id and e.type=2 
        		where e.pic_url!='' group by a.id";
        //增加搜索条件
        if (!empty($type)) {
            $sql .= " and b.education_auth=$type";
        }
        if (!empty($nickname)) {
            $sql .= " and b.nickname like '%$nickname%'";
        }
        if (!empty($statu)) {
            $sql .= " and a.statu=$statu";
        }
        $sql.=" order by a.register_time desc";
        $data = $model->query($sql);
        $count = count($data);
        $Page = new \Think\Page($count, 10);// 实例化分页类
        $show = $Page->show();// 分页显示输出
        $sql .= " limit $Page->firstRow,$Page->listRows";
        $list = $model->query($sql);
        $this->assign('data', $list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
//        var_dump($list);
        $this->assign('page', $show);
        $this->display();
    }


    //财务管理
    public function finance_manage()
    {
        $model = new Model();
        $sql = "select sum(total_amount) as total from wl_pay_record";
        $total = $model->query($sql);
        $total_amount['total'] = sprintf("%.2f",$total[0]['total']);
        $msql = "select sum(total_amount) as total from wl_pay_record where time between adddate(curdate(),-29) and adddate(curdate(),1)";
        $mdata = $model->query($msql);
        $total_amount['mtotal'] = sprintf("%.2f",$mdata[0]['total']);
        //求出月收入
        $wsql = "select sum(total_amount) as total from wl_pay_record where time between adddate(curdate(),-6) and adddate(curdate(),1)";
        $wdata = $model->query($wsql);
        $total_amount['wtotal'] = sprintf("%.2f",$wdata[0]['total']);
        //求出周收入
        $this->assign('total_amount',$total_amount);//总收入

        $rtotal_amount = $this->get_total(1);
        $this->assign('rtotal_amount',$rtotal_amount);//认证费总收入

        $htotal_amount = $this->get_total1(1);
        $this->assign('htotal_amount',$htotal_amount);//会员费总收入
        $this->display();
    }

    public function get_total($type)
    {
        $model = new Model();
        $sql = "select sum(total_amount) as total from wl_pay_record where type=$type";
        $data = $model->query($sql);
       $result['total'] = sprintf("%.2f",$data[0]['total']);

        $msql = "select sum(total_amount) as total from wl_pay_record where time between adddate(curdate(),-29) and adddate(curdate(),1) and type=$type";
        $mdata = $model->query($msql);
        $result['mtotal'] = sprintf("%.2f",$mdata[0]['total']);

        $wsql = "select sum(total_amount) as total from wl_pay_record where time between adddate(curdate(),-6) and adddate(curdate(),1) and type=$type";
        $wdata = $model->query($wsql);
        $result['wtotal'] = sprintf("%.2f",$wdata[0]['total']);
        return $result;
    }

    public function get_total1($type)
    {
        $model = new Model();
        $sql = "select sum(total_amount) as total from wl_pay_record where type!=$type";
        $data = $model->query($sql);
        $result['total'] = sprintf("%.2f",$data[0]['total']);

        $msql = "select sum(total_amount) as total from wl_pay_record where time between adddate(curdate(),-29) and adddate(curdate(),1) and type!=$type";
        $mdata = $model->query($msql);
        $result['mtotal'] = sprintf("%.2f",$mdata[0]['total']);

        $wsql = "select sum(total_amount) as total from wl_pay_record where time between adddate(curdate(),-6) and adddate(curdate(),1) and type!=$type";
        $wdata = $model->query($wsql);
        $result['wtotal'] = sprintf("%.2f",$wdata[0]['total']);
        return $result;
    }

    //会员费设置
    public function member_setting()
    {
        echo 'this is setting';
    }

    //查看用户信息
    public function userinfo()
    {
        $model = new Model();
        $id = I('get.id');
        $sql = "select pic_url,type from wl_images where user_id=$id and type=1";
        $shouchi = $model->query($sql);
        $this->assign('shouchi', $shouchi[0]);
        $zsql = "select pic_url,type from wl_images where user_id=$id and type=11";
        $zhengmian = $model->query($zsql);
        $this->assign('zhengmian', $zhengmian[0]);
        $city = "select c.region_name,d.user from wl_region a,wl_userinfo b,wl_region c,wl_user d where b.user_id=$id and b.city=a.id and a.parent_id=c.id and d.id=$id";
        $data = $model->query($city);
        $this->assign('data', $data[0]);
        $this->display();
    }

    //查看用户详细信息
    public function userinfo_detail()
    {
        $model1 = D('income');
        $income = $model1->where(1)->select();
        $model2 = D('marry');
        $marry = $model2->where(1)->select();
        $this->assign('marry',$marry);
        $this->assign('income',$income);

        $model = new Model();
        $id = I('get.id');
        $sql = "select * from wl_userinfo where user_id=$id";
        $data = $model->query($sql);
//        var_dump($data);
        $isql = "select * from wl_images where user_id=$id and type in(2,3,4) order by type asc";
        $images = $model->query($isql);
        $this->assign('images', $images);
        $data[0]['self_introduction'] = base64_decode($data[0]['self_introduction']);
        $data[0]['mate_selection'] = base64_decode($data[0]['mate_selection']);
        $this->assign('data', $data[0]);
//        var_dump($data);
        $model1 = D('region');
        $condition['region_type'] = 1;
        $sheng = $model1->where($condition)->select();
        $this->assign('sheng',$sheng);
        //获取个人照片
        $sql = "select pic_url from wl_images where user_id=$id";
        $life = $model->query($sql);
        $this->assign('life',$life);
        $this->display();
    }

    //编辑用户资料
    public function edit_userinfo()
    {
        $model = D('userinfo');
        $user_id = I('post.user_id');
        $data = I('post.');
        $data['self_introduction'] = base64_encode($data['self_introduction']);
        $data['mate_selection'] = base64_encode($data['mate_selection']);
        $condition['user_id'] = $user_id;
        $model->where($condition)->save($data);
        redirect(U('index/index'));
    }


    //添加用户
    public function add_user()
    {
        $model = D('income');
        $income = $model->where(1)->select();
        $Model = D('marry');
        $marry = $Model->where(1)->select();
        $model1 = D('region');
        $condition['region_type'] = 1;
        $sheng = $model1->where($condition)->select();
        $this->assign('sheng',$sheng);
        $this->assign('marry',$marry);
        $this->assign('income',$income);
        $this->display();
    }

    /*
     * 将新增用户写入数据库
     * @param array $data 将用户信息写入user表
     * @param string $res 写入数据库之后返回的用户id,后面有两次调用
     * @param array $userinfo 写入userinfo表中的信息
     * 其中有两次调用七牛上传图片
     * return 跳转到主页面
     */
    public function write_add_user()
    {

        $model = D('user');
        $user = I('post.user');
        $password = md5(I('post.password'));
        $data = array(
            'user' => $user,
            'password' => $password,
            'register_time' => date('y-m-d,H:i:s', time())
        );
        $res = $model->data($data)->add();
            if(!empty($_FILES['myphoto'])) {
                $this->upload_curl_pic($_FILES['myphoto']['tmp_name'][0],$res,5);
            }

        if(!empty($_FILES['pic'])) {
            $file['pic'] = $_FILES['pic'];
            $setting=C('UPLOAD_SITEIMG_QINIU');
            $Upload = new \Think\Upload($setting);
            $info = $Upload->upload($file);
            $url = $info['pic']['url'];
        }
        $userinfo = array(
            'nickname' => I('post.nickname'),
            'self_introduction' => base64_encode(I('post.self_introduction')),
            'birthday' => I('post.birthday'),
            'constellation' => I('post.constellation'),
            'height' => I('post.height'),
            'mate_selection' => base64_encode(I('post.mate_selection')),
            'annual_income'=>I('post.annual_income'),
            'hope_marry_time'=>I('post.hope_marry_time'),
            'city'=>I('post.city'),
            'family_background' => I('post.family_background'),
            'career_planning' => I('post.career_planning'),
            'emotion' => I('post.emotion'),
            'sex'=>I('post.sex'),
            'header_pic'=>$url
        );
        $userinfo['user_id'] = $res;
        $uid = rand(1000,9999);
        $userinfo['uid'] = $uid.$res;
        $usermodel = D('userinfo');
        $usermodel->data($userinfo)->add();
        redirect(U('index/index?type=1'));
    }


    //举报管理
    public function report_manage()
    {
        $model = D('report');
        $sql = "select * from wl_report";
        $data = $model->query($sql);
        $count = count($data);
        $Page = new \Think\Page($count, 5);// 实例化分页类
        $show = $Page->show();// 分页显示输出
        $sql .= " limit $Page->firstRow,$Page->listRows";
        $list = $model->query($sql);
        $this->assign('data', $list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
        $this->assign('page',$show);
        $this->display();
    }

    //举报详情
    public function report_detail()
    {
        $model = D('report');
        $id = I('get.id');
        $condition['id'] = $id;
        $data = $model->where($condition)->select();
        $this->assign('data',$data[0]);
        $this->display();
    }

    //查看认证
    public function check_auth()
    {
        $model = new Model();
        $id = I('get.id');
        $sql = "select a.user,a.type,d.region_name,e.pic_url as shouchi,f.pic_url as zhengmian from wl_user a left join wl_userinfo b on a.id=b.user_id left join wl_region c on b.city=c.id
       left join wl_region d on c.parent_id=d.id left join wl_images e on a.id=e.user_id and e.type=1 left join wl_images f on a.id=f.user_id and f.type=11 where a.id=$id";
        $data = $model->query($sql);
        $data[0]['id'] = $id;
        $this->assign('data', $data[0]);
        $this->display();
    }

    //查看房产认证
    public function check_house_auth()
    {
        $model = new Model();
        $id = I('get.id');
        $sql = "select a.house_auth as type,b.pic_url as fangchan from wl_userinfo a left join wl_images b on a.user_id=b.user_id where a.user_id=$id and b.type=3";
        $data = $model->query($sql);
        $data[0]['id'] = $id;
        $this->assign('data', $data[0]);
        $this->display();
    }

    //查看车辆认证
    public function check_car_auth()
    {
        $model = new Model();
        $id = I('get.id');
        $sql = "select a.car_auth as type,b.pic_url as car from wl_userinfo a left join wl_images b on a.user_id=b.user_id where b.user_id=$id and b.type=4";
        $data = $model->query($sql);
        $data[0]['id'] = $id;
        $this->assign('data', $data[0]);
        $this->display();
    }

    //查看学历认证
    public function check_education_auth()
    {
        $model = new Model();
        $id = I('get.id');
        $sql = "select a.education_auth as type,b.pic_url as education from wl_userinfo a left join wl_images b on a.user_id=b.user_id where b.user_id=$id and b.type=2";
        $data = $model->query($sql);
        $data[0]['id'] = $id;
        $this->assign('data', $data[0]);
        $this->display();
    }

    //通过认证
    public function access_auth()
    {
        $id = I('post.id');
        $type = I('post.type');
        if ($type == 'identity') {
            $model = D('user');
            $data = array(
                'type' => 2,
                'identity_auth_time' => date('y-m-d H:i:s', time()),
            );
            $condition['id'] = $id;
            $res = $model->where($condition)->save($data);
            $url = U('index/auth_manage');
        } else {
            $model = D('userinfo');
            $condition['user_id'] = $id;
            if ($type == 'house') {
                $data = array(
                    'house_auth' => 2,
                    'house_auth_time' => date('y-m-d H:i:s', time()),
                );
                $url = U('index/house_auth_manage');
            }
            if ($type == 'car') {
                $data = array(
                    'car_auth' => 2,
                    'car_auth_time' => date('y-m-d H:i:s', time()),
                );
                $url = U('index/car_auth_manage');
            }
            if ($type == 'education') {
                $data = array(
                    'education_auth' => 2,
                    'education_auth_time' => date('y-m-d H:i:s', time()),
                );
                $url = U('index/education_auth_manage');
            }
            $res = $model->where($condition)->save($data);
        }
        if (!empty($res)) {
            redirect($url);
        }

    }

    //驳回认证
    public function reject_auth()
    {
        $id = I('post.id');
        $type = I('post.type');
        $reject_content = I('post.reject_content');
        if ($type == 'identity') {
            $model = D('user');
            $data = array(
                'type' => 3,
                'reject_content'=>$reject_content
            );
            $condition['id'] = $id;
            $res = $model->where($condition)->save($data);
        } else {
            $model = D('userinfo');
            $condition['user_id'] = $id;
            if ($type == 'house') {
                $data = array(
                    'house_auth' => 3,
                    'house_reject_content' =>$reject_content
                );
            }
            if ($type == 'car') {
                $data = array(
                    'car_auth' => 3,
                    'car_reject_content' =>$reject_content
                );
            }
            if ($type == 'education') {
                $data = array(
                    'education_auth' => 3,
                    'education_reject_content' =>$reject_content
                );
            }
            $res = $model->where($condition)->save($data);
        }
        if (!empty($res)) {
            $result = array(
                'statu'=>1,
                'msg'=>'reject success',
            );
        }else{
            $result = array(
                'statu'=>2,
                'msg'=>'reject failed',
            );
        }
         R('sms/send_sms', array(15527959893,3));//调用短信接口发送身份验证失败短信
        $this->ajaxReturn($result);
    }

    public function ceshi()
    {
        R('sms/send_sms', array(15527959893,3));
    }

   //ajax获取省份对应的城市
    public function ajax_get_city()
    {
       $model = new Model();
        $id = I('post.shengfen');
        $sql = "select a.* from wl_region a,wl_region b where a.parent_id=b.id and a.parent_id=$id";
        $data = $model->query($sql);
       $this->ajaxReturn($data,'json');
    }

    //根据生日获取对应的星座
    public function ajax_constellation()
    {
        $birthday = I('post.birthday');
        $data = explode('-',$birthday);
        $month = $data[1];
        $day = $data[2];
        $star = parent::getstar($month,$day);
        $result = array(
            'statu'=>1,
            'star'=>$star,
        );
        $this->ajaxReturn($result);

    }


}